package com.huatai.datadevelop.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.datadevelop.entity.TokenConfigEntity;
import com.huatai.datadevelop.mapper.TokenConfigMapper;
import com.huatai.datadevelop.utils.PageUtils;
import com.huatai.datadevelop.utils.Query;
import com.huatai.datadevelop.utils.RequestApiTokenUtil;
import com.huatai.datadevelop.vo.KeyValueVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;;
import java.util.List;
import java.util.Map;

/**
 * @author hecai
 * @description: TODO
 * @date 2022/11/21 19:25
 * @Version 1.0
 */
@Service
public class TokenConfigService extends ServiceImpl<TokenConfigMapper, TokenConfigEntity> {

    @Autowired
    private RequestApiTokenUtil requestApiTokenUtil;

    @Autowired
    private TokenConfigService tokenConfigService;

    /**
     * 获取token数据-->post请求
     * @param id
     * @return
     */
    public String  getApiTokenByBody(Integer id){
        TokenConfigEntity configEntity = tokenConfigService.getById(id);
        String apiTokenByBody = requestApiTokenUtil.getApiTokenByBodyOfForm(configEntity);
        return apiTokenByBody;
    }

     /**
     * 获取token数据-->post请求
     * @param configEntity
     * @return
     */
    public String  getApiTokenCheck(TokenConfigEntity configEntity){
        String method = configEntity.getMethod();
        if(method.equalsIgnoreCase("GET")){
            String parameter = configEntity.getParameter();
            if(parameter==null){
                throw new ServiceException("请求get参数有异常");
            }
        }else {
            JSONObject bodyData = configEntity.getBodyData();
            if(bodyData==null || bodyData.size()<0){
                throw new ServiceException("请求body参数有异常");
            }
            configEntity.setBody(JSON.toJSONString(bodyData));
        }

        JSONObject heardData = configEntity.getHeardData();
        if(heardData!=null && heardData.size()>0){
            configEntity.setHeard(JSON.toJSONString(heardData));
        }
        String apiTokenByBody = requestApiTokenUtil.getApiTokenByBodyOfForm(configEntity);
        return apiTokenByBody;
    }

    /**
     * 新增或更新
     * @param data
     * @return
     */
    public Boolean saveOrUpdateTokenConfig(TokenConfigEntity data){
        JSONObject bodyData = data.getBodyData();
        if(bodyData!=null&&bodyData.size()>0){
            data.setBody(JSON.toJSONString(bodyData));
        }
        JSONObject heardData = data.getHeardData();
        if(heardData!=null && heardData.size()>0){
            data.setHeard(JSON.toJSONString(heardData));
        }
        return super.saveOrUpdate(data);
    }

    /**
     * 删除或批量删除
     * @param ids
     * @return
     */
    public Boolean deleteByIds(List<Integer> ids){
        return super.removeByIds(ids);
    }

    /**
     * 分页获取数据
     * @param params
     * @return
     */
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        IPage<TokenConfigEntity> page = this.page(
                new Query<TokenConfigEntity>().getPage(params),
                new QueryWrapper<TokenConfigEntity>().like(StringUtils.isNotBlank(name),"name", name)
        );
        //方便前端展示，将body参数转化成json格式
        if(page.getPages()>0){
            List<TokenConfigEntity> records = page.getRecords();
            records.forEach(p->{
                String body = p.getBody();
                p.setBodyData(JSON.parseObject(body));
                String heard = p.getHeard();
                p.setHeardData(JSON.parseObject(heard));
            });
        }
        return new PageUtils(page);
    }

    /**
     * 获取token列表
     * @return
     */
    public List<KeyValueVO> listToken(){
        List<KeyValueVO> voList = new ArrayList<>();
        List<TokenConfigEntity> list = this.list();
        list.forEach(data->{
            voList.add(new KeyValueVO(data.getId().toString(),data.getName()));
        });
        return voList;
    }

}
